/*
 * ============================================================================
 *
 *       Filename:  ceshi.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2013年06月19日 11时46分06秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>

int first_bit_fwd(uint64_t dword)
{
	int num;
	__asm__(".set push \n\t"
		".set mips64 \n\t"
		"dclz %0,%1 \n\t"
		".set pop \n\t"
		: "=r" (num)
		: "r" (dword));
	return 63 - num;
}

int first_bit_fwd_raw(uint64_t dword)
{
	uint64_t tmp = 1, i = 0;
	
	for(i = 63; i >= 0; i--) {
		if(dword & (tmp<<i))
			return i;
	}
	return -1;
}

int main()
{
	uint64_t data=1;
	int a,b;
	while(1) {
		printf("running:%lu\r",data);
		a = first_bit_fwd(data);
		b = first_bit_fwd_raw(data);
		if(a != b) {
			printf("ERR:no equal:%lu\n",data);
			break;
		}
		data++;
	}
}
